package com.ncp.model.biz.flex;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.ncp.model.dao.DB;
import com.ncp.model.dao.DbConnection;

public class FlexService {
	
	private Connection conn=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	/**
	 * 得到服务器的ip地址
	 * @return ip地址
	 */
	public String getServerIP(){
		String ip="";
		String sql="select serverip from FLEX where id=1";
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				ip=rs.getString(1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
					rs=null;
				}
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
			new DB().closeSQL(conn, ps);
		}
		return ip;
	}
	
	/**
	 * 更新服务器的ip地址，保持纪录只有第一条
	 * @param ip最新的ip地址
	 */
	public void updateServerIP(String ip){
		String sql="update FLEX set serverip='"+ip+"' where id=1";
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			new DB().closeSQL(conn, ps);
		}
	}
}
